﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Models;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Mvc.Models;

namespace Vit.Mvc.Providers
{
    public class DefaultPortalProvider : IPortalProvider
    {
        #region IPortalProvider Members

        public IEnumerable<IPortal> GetPortals()
        {
            VitDB db = new VitDB();
            var query = from o in db.Portals
                        where o.Status == (int)Status.Actived
                        select o;
            return query;
        }

        public IPortal GetPortalByName(string name)
        {
            throw new NotImplementedException();
        }

        public void AddPortal(IPortal portal)
        {
            throw new NotImplementedException();
        }

        public void UpdatePortal(IPortal portal)
        {
            throw new NotImplementedException();
        }

        public void DeleteProtal(IPortal portal)
        {
            throw new NotImplementedException();
        }

        public IWebSite GetWebSiteByName(string name)
        {
            throw new NotImplementedException();
        }

        public void AddWebSiteToPortal(IWebSite website, IPortal portal)
        {
            throw new NotImplementedException();
        }

        public void UpdateWebSite(IWebSite website)
        {
            throw new NotImplementedException();
        }

        public void DeleteWebSiteFromPortal(IWebSite website, IPortal portal)
        {
            throw new NotImplementedException();
        }

        public void SetWebSitePortal(IWebSite website, IPortal portal)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IProvider Members

        public string Name
        {
            get { return "DefaultPortalProvider"; }
        }

        #endregion

        #region IPortalProvider Members

        public IWebSite GetWebSite(string url)
        {
            VitDB db = new VitDB();
            var site = (from o in db.WebSites
                        where o.Status == (int)Status.Actived // && url.StartsWith(o.Url)
                        select o).FirstOrDefault();

            return site;
        }

        public IPage GetPage(string url)
        {
            VitDB db = new VitDB();
            var pages = from o in db.WebPages
                        join s in db.WebSites on o.SiteID equals s.SiteID
                        where s.Status == (int)Status.Actived // && url.StartsWith(s.Url)
                        select o;

            foreach (var p in pages)
            {
                if (Regex.IsMatch(url, p.UrlPattern))
                    return p;
            }
            return null;
        }

        #endregion

        #region IPortalProvider Members

        public ITheme GetCurrentTheme()
        {
            var user = WebUnity.Current.MembershipService.Run<IUser>(p => p.GetCurrentUser(), o => o != null);
            if (user != null)
            {
                User dbUser = user as User;
                return dbUser.Theme;
            }
            return new Theme() { Name = "Default" };
        }

        public ISkin GetCurrentSkin()
        {
            var user = WebUnity.Current.MembershipService.Run<IUser>(p => p.GetCurrentUser(), o => o != null);
            if (user != null)
            {
                User dbUser = user as User;
                return dbUser.Skin;
            }
            return new Skin() { Name = "Default" };
        }

        #endregion

        #region IPortalProvider Members


        public IEnumerable<IWebModule> GetWebModules()
        {
            return WebUnity.Current.UnityContainer.ResolveAll<IWebModule>();
        }

        public IWebModule GetWebModuleByID(Guid id)
        {
            return WebUnity.Current.UnityContainer.Resolve<IWebModule>(id.ToString());
        }

        #endregion

        #region IPortalProvider Members


        public EditMode GetCurrentUserMode()
        {
            if (HttpContext.Current.Session != null && HttpContext.Current.Session["EditMode"] != null)
                return (EditMode)Convert.ToInt32(HttpContext.Current.Session["EditMode"]);
            return EditMode.View;
        }

        #endregion
    }
}
